//
//  HWBalanceMingxiViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/10/24.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWBalanceMingxiViewController.h"
#import "HWshouzhiMXCell.h"

@interface HWBalanceMingxiViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, weak) UIScrollView *titleScrollView;
@property (strong,nonatomic) UIImageView  *xiantiaoimageview;
@property (nonatomic,strong) UIButton *selectButton;
@property (nonatomic, strong) NSMutableArray *titleButtons;
@property (nonatomic,strong)  NSMutableArray *biaotiArray;
@property (strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) UITableView *rightTableView;
@property(strong,nonatomic) NSString *panduan;





@end

@implementation HWBalanceMingxiViewController
- (NSMutableArray *)titleButtons
{
    if (_titleButtons == nil) {
        _titleButtons = [NSMutableArray array];
    }
    return _titleButtons;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
   // HWBalanceMingxiViewController
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 1.添加标题滚动视图
    [self setupTitleScrollView];
    
    
    [self  setupAllChildViewController];
    
    [self setupAllTitle];
    
    [self setupUI];
    
    
    
    
}

#pragma mark - 添加标题滚动视图
- (void)setupTitleScrollView
{
    // 创建titleScrollView
    UIScrollView *titleScrollView = [[UIScrollView alloc] init];
    CGFloat y = 0;
    titleScrollView.backgroundColor = [UIColor redColor];
    titleScrollView.frame = CGRectMake(0, y, self.view.bounds.size.width, 40);
    // titleScrollView.backgroundColor = [UIColor redColor];
    titleScrollView.backgroundColor = RGB(241,242, 244);
    
    [self.view addSubview:titleScrollView];
    _titleScrollView = titleScrollView;
    
    
    
    
}

- (void)setupAllChildViewController
{
    self.biaotiArray = [[NSMutableArray alloc]init];
    [self.biaotiArray addObject:@"收入明细"];
    [self.biaotiArray addObject:@"支出明细"];

}
#pragma mark - 设置所有标题
- (void)setupAllTitle
{
    // 已经把内容展示上去 -> 展示的效果是否是我们想要的(调整细节)
    // 1.标题颜色 为黑色
    // 2.需要让titleScrollView可以滚动
    
    
    
    
    // 添加所有标题按钮
    NSInteger count = self.biaotiArray.count;
    CGFloat btnW = 100;
    CGFloat btnH = self.titleScrollView.bounds.size.height;
    CGFloat btnX = 0;
    for (NSInteger i = 0; i < count; i++) {
        
        UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        titleButton.tag = i;
        titleButton.backgroundColor = [UIColor whiteColor];
        //  UIViewController *vc = self.childViewControllers[i];
        [titleButton setTitle:self.biaotiArray[i] forState:UIControlStateNormal];
        CGFloat  jiaju =  (kScreenW - count *100) / (count+ 1);
        
        btnX = i * btnW + jiaju*(i+1);
        btnX = i*(kScreenW/2);
        btnW = (kScreenW/2);
        titleButton.frame = CGRectMake(btnX, 0, btnW, btnH);
        [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //    titleButton.backgroundColor = [UIColor redColor];
        titleButton.titleLabel.font = [UIFont systemFontOfSize:13];
        // 监听按钮点击
        [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        // 把标题按钮保存到对应的数组
        [self.titleButtons addObject:titleButton];
        
    
        
        if (i == 0) {
            [self titleClick:titleButton];
            

            
        }
        
        [self.titleScrollView addSubview:titleButton];
        
        
    }
    
    
    
    UIView *fengexianView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 1)];
      fengexianView.backgroundColor = RGB(232, 232, 242);
   
    [self.titleScrollView  addSubview:fengexianView ];
    
    
     UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 39, kScreenW, 1)];
     lineView.backgroundColor = RGB(232, 232, 242);
     [self.titleScrollView  addSubview:lineView];
    
    
    UIView *fengexain  = [[UIView alloc]initWithFrame:CGRectMake((kScreenW/2), 0, 1, 40)];
    fengexain.backgroundColor = RGB(232, 232, 242);
    [self.titleScrollView addSubview:fengexain];
    
    
}


#pragma mark - 处理标题点击
- (void)titleClick:(UIButton *)button
{
    NSInteger i = button.tag;
    
    // 1.标题颜色 变成 红色
     [self selButton:button];
    
 //    CGRectMake(btnX+25, btnH-2-6, 50, 2)
    
   
    // 2.把对应子控制器的view添加上去
//    [self setupOneViewController:i];
//
//    // 3.内容滚动视图滚动到对应的位置
//    CGFloat x = i * [UIScreen mainScreen].bounds.size.width;
//    self.contentScrollView.contentOffset = CGPointMake(x, 0);
    
    if (i == 0) {
        
        self.mainaTableVie.hidden  =NO;
        self.rightTableView.hidden = YES;
        self.panduan = @"1";
        [self.mainaTableVie reloadData];
        
        
    } else {
        
        
        self.mainaTableVie.hidden  =YES;
        self.rightTableView.hidden = NO;
        self.panduan = @"2";
        [self.rightTableView reloadData];
         
        
    }
    
}



#pragma mark - 选中标题
- (void)selButton:(UIButton *)button
{
    [self.selectButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:RGB(26, 113, 255) forState:UIControlStateNormal];
    self.selectButton = button;
    
//    NSInteger i = button.tag;
//    NSInteger count = self.biaotiArray.count;
//    CGFloat  jiaju =  (kScreenW - count *100) / (count+ 1);
//    CGFloat btnW = 100;
//    CGFloat btnX = i * btnW + jiaju*(i+1);
//    self.xiantiaoimageview.frame = CGRectMake(btnX+30, 40-2, 40, 2);
    
    
}





#pragma mark -----------------  添加两个Tabelview

- (UITableView *)mainaTableVie {

    if (_mainaTableVie == nil
        ) {

         _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, kScreenW,kScreenH-40-CF_NavHeight-127) style:UITableViewStylePlain];
         _mainaTableVie.delegate = self;
         _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        

    }
    return   _mainaTableVie ;
}

- (UITableView *)rightTableView {
    if (_rightTableView == nil) {
        
           _rightTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, kScreenW,kScreenH-40-CF_NavHeight-127) style:UITableViewStylePlain];
           _rightTableView.delegate = self;
           _rightTableView.dataSource = self;
          _rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    
    return  _rightTableView;
}




- (void)setupUI {
    self.view.backgroundColor = [UIColor redColor];
    self.mainaTableVie.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mainaTableVie];
    
     self.rightTableView.backgroundColor = [UIColor whiteColor];
     self.rightTableView.hidden = YES;
     [self.view addSubview:self.rightTableView];
    
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if ([self.panduan isEqualToString:@"1"]) {
        return 10;
    } else {
        
        return 5;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellID";
    
    HWshouzhiMXCell  *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[HWshouzhiMXCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        
        
        
    }
    cell.selectionStyle   =  UITableViewCellSelectionStyleNone;
    cell.zuixianbianfengexxianView.hidden = YES;
    if ([self.panduan isEqualToString:@"1"]) {
        if (indexPath.row == 9) {
             cell.zuixianbianfengexxianView.hidden = NO;
        }
        
        
    } else {
        
        if (indexPath.row == 4) {
                   cell.zuixianbianfengexxianView.hidden = NO;
        }
    }

    return  cell;
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    return  36;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    
    
    return  36;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 36)];
    view.backgroundColor = RGB(233, 241, 255);
    NSArray *arr = @[@"时间",@"平台币",@"备注"];
    for (int i = 0; i < 3; i++) {
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(kScreenW/3*i, 0, kScreenW/3, 36)];
        label.text = arr[i];
        label.font = [UIFont systemFontOfSize:13];
        label.textColor = RGB(51, 51, 51);
        label.textAlignment = NSTextAlignmentCenter;
        [view addSubview:label];
        
    }
    
    for (int i = 0 ; i < 2 ; i++) {
        UIView *shuxiangView = [[UIView alloc]initWithFrame:CGRectMake(kScreenW/3*(i+1), 0, 1, 36)];
        shuxiangView.backgroundColor = RGB(232, 232, 242);
        [view addSubview:shuxiangView];
    }
    
    return  view;
    
}

@end
